

<?php 

$DB = new DB();
$con = $DB->conDB();

// alle offenen Matches auslesen
$sql = "SELECT MatchID, MatchModeID, MatchTypeID
		FROM `Match`
		WHERE TeamWonID = -1 AND TimestampClosed = 0 AND ManuallyCheck = 0 AND Canceled = 0
		ORDER BY MatchID
		LIMIT 50
		";
$openMatches = $DB->multiSelect($sql);
//p($sql);
// f�r jedes Match auslesen ob alle submitted haben, dann ergebnis eintragen
if(is_array($openMatches) && count($openMatches) > 0){
	foreach($openMatches as $k =>$v){
		$matchID = $v["MatchID"];
		$matchModeID = $v["MatchModeID"];
		$matchTypeID = $v["MatchTypeID"];
		
		$sql = "SELECT *
		FROM `MatchDetails`
		WHERE MatchID = ".(int) $matchID." AND Submitted = 1 AND SubmissionFor != 0 AND SubmissionTimeStamp != 0
		";
		$countSubmitted = $DB->countRows($sql);
		$matchDetails = $DB->multiSelect($sql);
		//p($sql);
		$ret["debug"] .= p($matchDetails,1);
		// wenn mind 8 submitted haben
		if($countSubmitted >= (10-MatchDetails::screenshotGrenze)){
			$MatchDetails = new MatchDetails();
			$strangeSubmission = $MatchDetails->checkForStrangeWinLoseSubmission($matchID, $matchDetails);
			//p($strangeSubmission);
			// Wenn gegenstimmen kleiner als 3, dann automatisch behandeln
			if($strangeSubmission['result'] <= MatchDetails::screenshotGrenze){
				
				$teamWonID = $MatchDetails->getTeamWon($matchID);
				//p($teamWonID);
				$teamWonID = $teamWonID['teamWonID'];
				
				$sql = "UPDATE `Match`
				SET TeamWonID = ".(int) $teamWonID.", TimestampClosed = ".time()."
				WHERE MatchID = ".(int) $matchID."
					";
				$DB->update($sql);
				//p($sql);
				
				// EloChanges eintragen
				$retMatchDetails = $MatchDetails->insertEloChangesForMatchDetails($matchID, $teamWonID);
				//$ret['debug'] .= $retMatchDetails['debug'];
				$ret['debug'] .= "MatchID: ".$matchID." -> teamWonID: ".$teamWonID;
				
				
				// Elo von allen Usern anpassen
				$UserElo = new UserElo();
				$retChange = $UserElo->changeUserEloAfterMatchSubmit($matchModeID, $matchTypeID, $teamWonID, $matchID);
				$ret['debug'] .= $retChange['debug'];
				//p($ret);
				
				
				
				// WIN/Lose/Leaves und neue WINRATE eintragen
				$UserStats = new UserStats();
				//$playerData = $MatchDetails->getAllPlayersInMatch($matchID,"md.SteamID, md.TeamID");
				//$playerData = $playerData['data'];
				$playerData = $matchDetails;
				$ret['debug'] .= $playerData['debug'];
				if(is_array($playerData) && count($playerData) > 0){
					foreach($playerData as $k => $v){
						if($teamWonID == $v['TeamID']){
							$result=1;
						}
						else{
							$result = -1;
						}
						
						// allgemeine stats updaten
						$retUpdate = $UserStats->updateGeneralUserStats($v['SteamID'], $result, $matchID);
						$ret['debug'] .= $retUpdate['debug'];
						// MatchMode Spezifische Stats updaten
						$retEloStats = $UserElo->updateUserEloStats($v['SteamID'], $result, $matchTypeID, $matchModeID);
						$ret['debug'] .= $retEloStats['debug'];
					}
				}
				
				
				p("Matches erfolgreich aktualisert!<br>\n".$ret['debug']);
			}
			// manuell kontrollieren anhand von Screenshots
			else{
				$sql = "UPDATE `Match`
						SET ManuallyCheck = 1
						WHERE MatchID = ".(int) $matchID."
					";
				//p($sql);
				$DB->update($sql);
				p("Manueller Check!");
			}	
		}
		else{
			 echo "keine 10 MatchID:".$matchID;
		}
	}
}
else {
	 echo "keine offenen Matches";
}


?>